package com.seari.tunnel.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seari.tunnel.entity.FeatureDevice;
import com.seari.tunnel.entity.FeatureDevicePage;
import com.seari.tunnel.service.IEmergencyMaterialsService;

@Controller
public class EmergencyController {
	private final String CZYA_PAGE = "/czya";
	private final String ZDGC_PAGE = "/zdgc";
	private final String XFAQ_PAGE = "/xfaq";
	private final String FTFX_PAGE = "/ftfx";
	private final String RYDL_PAGE = "/rydl";
	private final String RYSS_PAGE = "/ryss";
	private final String RYRQ_PAGE = "/ryrq";
	private final String SGBL_PAGE = "/sgbl";
	private final String BYFX_PAGE = "/byfx";
	private final String YJYL_PAGE = "/yjyl";
	private final String YJWZ_PAGE = "/yjwz";
	
	@Autowired
	IEmergencyMaterialsService emergencyMaterialsService;
	
	@RequestMapping(value="/czya")
	public ModelAndView czya(){
		
		return new ModelAndView(CZYA_PAGE);
	}
	
	@RequestMapping(value="/zdgc")
	public ModelAndView zdgc(){
		
		return new ModelAndView(ZDGC_PAGE);
	}
	
	@RequestMapping(value="/xfaq")
	public ModelAndView xfaq(){
		
		return new ModelAndView(XFAQ_PAGE);
	}
	
	@RequestMapping(value="/ftfx")
	public ModelAndView ftfx(){
		
		return new ModelAndView(FTFX_PAGE);
	}
	
	@RequestMapping(value="/rydl")
	public ModelAndView rydl(){
		
		return new ModelAndView(RYDL_PAGE);
	}
	
	@RequestMapping(value="/ryss")
	public ModelAndView ryss(){
		
		return new ModelAndView(RYSS_PAGE);
	}
	
	@RequestMapping(value="/ryrq")
	public ModelAndView ryrq(){
		
		return new ModelAndView(RYRQ_PAGE);
	}
	
	@RequestMapping(value="/sgbl")
	public ModelAndView sgbl(){
		
		return new ModelAndView(SGBL_PAGE);
	}
	
	@RequestMapping(value="/byfx")
	public ModelAndView byfx(){
		
		return new ModelAndView(BYFX_PAGE);
	}
	
	@RequestMapping(value="/yjyl")
	public ModelAndView yjyl(){
		
		return new ModelAndView(YJYL_PAGE);
	}
	@RequestMapping(value="/yjyl2")
	public ModelAndView yjyl2(){

		return new ModelAndView("/yjyl2");
	}
	@RequestMapping(value="/yjyl3")
	public ModelAndView yjyl3(){

		return new ModelAndView("/yjyl3");
	}
	
	@RequestMapping(value="/yjwz")
	public ModelAndView yjwz(){
		
		return new ModelAndView(YJWZ_PAGE);
	}
	
	@RequestMapping(value = "/loadMaterialList")
	@ResponseBody
	public Object getMaterialList(@RequestParam("rows")int rows,@RequestParam("page")int page,@RequestParam(value="_search",required=false) boolean search,@RequestParam(value="searchString",required=false) String searchString) {
		FeatureDevicePage pages = new FeatureDevicePage();
		List<FeatureDevice> data = emergencyMaterialsService.getEmergencyMaterials();
		int rowCount = 0;
		if(data.size()%rows==0){
			rowCount = data.size()/rows;
		}else{
			rowCount = data.size()/rows+1;
		}
		List<FeatureDevice> cdata = new ArrayList<FeatureDevice>();
		for(int i=0;i<rows;i++){
			if(i+rows*(page-1)<data.size()){
			cdata.add(data.get(i+rows*(page-1)));
			}
		}
		pages.setPage(page);
		pages.setRecords(data.size());
		pages.setTotal(rowCount);
		pages.setRows(cdata);
		
		if(search==true){
			List<FeatureDevice> searchdata=new ArrayList<FeatureDevice>();
			try {
				searchString=new String(searchString.getBytes("ISO8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			String str[]=searchString.split("&");
			for(int i=0;i<str.length;i++){
				searchdata.clear();
				for(FeatureDevice sa:data){
					if(sa.getBrand().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(String.valueOf(sa.getCount()).indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getDescription().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getModel().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getName().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getOper().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}else if(sa.getUnit().indexOf(str[i])!=-1){
						searchdata.add(sa);
						continue;
					}
				}
				data=new ArrayList<FeatureDevice>(searchdata);
			}
			
			return searchdata;
		}
		return pages;
	}
	
	@RequestMapping(value="/emergencyMaterialsEdit")
	@ResponseBody
	public ModelAndView emergencyMaterialsEdit(@ModelAttribute FeatureDevice featureDevice){
	    if("add".equals(featureDevice.getOper())){
	        emergencyMaterialsService.addEmergencyMaterials(featureDevice);
		    
		}
		else if("edit".equals(featureDevice.getOper())){
			emergencyMaterialsService.updateEmergencyMaterials(featureDevice);
		}
		else{
			int id=0;
			if(featureDevice.getId().contains(",")){
				id=Integer.parseInt(featureDevice.getId().replace(',', ' ').trim());
			}else{
				id=Integer.parseInt(featureDevice.getId());
			}
			emergencyMaterialsService.deleteById(id);
		}
		return new ModelAndView(YJWZ_PAGE);
	}
}
